'use client';

import { useEffect, useState } from 'react';
import { Button, Typography } from 'antd';
import { useRouter } from 'next/navigation';
import styles from './page.module.scss';
import { useConfig } from '../utils/useConfig';

const { Title, Paragraph } = Typography;

export default function HomePage() {
  const router = useRouter();
  const { config, loading } = useConfig();
  const [licenseError, setLicenseError] = useState<string | null>(null);
  const [licenseValid, setLicenseValid] = useState<boolean>(true);

  useEffect(() => {
    fetch('/api/license/status').then(async (res) => {
      const result = await res.json();
      if (result.status !== 200) {
        setLicenseError(result.message || '未授权，禁止登录');
        setLicenseValid(false);
      } else {
        setLicenseValid(true);
      }
    }).catch(() => {
      setLicenseError('无法获取授权状态');
      setLicenseValid(false);
    });
  }, []);

  const handleEnterSystem = () => {
    router.push('/admin');
  };

  const handleAIGCHelp = () => {
    // 这里可以跳转到帮助页面或打开帮助文档
    window.open('/docs/AIGC技术方案设计.md', '_blank');
  };

  // 如果正在加载配置，显示默认标题
  const pageTitle = config?.title || 'AIGC教学设计平台';

  return (
    <div className={styles.welcomeContainer}>
      {/* 简约背景元素 */}
      <div className={styles.subtleGrid}></div>
      
      <div className={styles.welcomeContent}>
        <Title level={1} className={styles.mainTitle}>
         {pageTitle}
        </Title>
        
        <Paragraph className={styles.subtitle}>
          智能生成 · 创新教学 · 未来教育
        </Paragraph>

        {licenseError && <div style={{ color: '#cf1322', marginBottom: 16, textAlign: 'center' }}>{licenseError}</div>}
        
        <div className={styles.buttonContainer}>
          <Button 
            type="primary" 
            size="large" 
            className={styles.enterButton}
            onClick={handleEnterSystem}
            disabled={!licenseValid}
          >
            进入系统
          </Button>
          
          <Button 
            size="large" 
            className={styles.helpButton}
            onClick={handleAIGCHelp}
            disabled={!licenseValid}
          >
            AIGC帮助
          </Button>
        </div>
      </div>
    </div>
  );
}